package com.tarena.lbs.activity.rpc;

import com.tarena.lbs.activity.api.ActivityApi;
import com.tarena.lbs.activity.api.pojo.ActivityDto;
import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.common.entity.Activity;
import com.tarena.lbs.common.entity.User;
import com.tarena.lbs.common.exception.ActivityNotFoundException;
import com.tarena.lbs.user.api.UserApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Component
@DubboService
public class ActivityApiImpl implements ActivityApi {
    @DubboReference
    private AttachApi attachApi;
    @Autowired
    private ActivityRepository repository;
    @DubboReference
    private UserApi userApi;
    @Autowired
    private ActivityService activityService;
    @Override
    public Boolean activityVisible(Integer userId, Integer activityId) {
        return false;
    }

    /**
     * 根据活动id获取活动详情
     * @author heyu
     * @date 2025年6月11日 14:42:15
     * @version 1.0
     * @param activityId
     * @return ActivityDto
     */
    @Override
    public ActivityDto getActivityById(Integer activityId) {
        ActivityPO po = repository.findById(activityId);
        if(po == null) return null;

        ActivityDto dto = new ActivityDto();
        BeanUtils.copyProperties(po, dto);

        if(StringUtils.isNotBlank(po.getImgIds())) {
            dto.setImgPics(convertImageIds(po.getImgIds()));
        }
        return dto;
    }

    @Override
    public boolean checkActivity(User user, Activity activity) {
        return false;
    }

    private List<String> convertImageIds(String imgIds) {
        return Arrays.stream(imgIds.split(","))
                .map(id -> attachApi.getUrlById(Integer.parseInt(id)))
                .collect(Collectors.toList());
    }

    @Override
    public boolean isUserTargetForActivity(Integer userId, Integer activityId) {
        // 第一个判断：校验活动是否存在
        ActivityPO activityPO = repository.findById(activityId);
        if (activityPO == null) {
            throw new ActivityNotFoundException("活动不存在，活动ID: " + activityId);
        }

        // 第二个判断：活动的目标人群是否为空
        String targetCustomer = activityPO.getTargetCustomer();
        if (StringUtils.isBlank(targetCustomer)) {
            return true;
        }

        // 不为空则调用用户服务UserApi查询用户的所属人群
        List<String> userGroups = userApi.getUserGroups(String.valueOf(userId));
        List<String> targetGroups = Arrays.asList(targetCustomer.split(","));

        // 判断活动人群是否在所属人群范围中
        return userGroups.stream().anyMatch(targetGroups::contains);
    }
}
